require('../../assets/css/index.less');
require('./register.less')
let dom = require('../../utils/dom.js');
let https=require('../../utils/https')
let Captcha = require('../../lib/captcha-mini/captcha-mini')
let capcode = '';
let validator=require('../../utils/validator')
dom.ready(function () {
    //验证码
    new Captcha({
        fontSize:30
    }).draw(dom.getEle('#captcha1'), function(r){
        capcode = r.toLowerCase;
    });

    //注册验证
    dom.getEle('.reg').addEventListener('click', function () {
        if (account.value == '') {
            alert('手机号不能为空')
        }
        else if (!validator.checkAcc(account.value)) {
            alert('手机号格式不对')
        }
        else if (vcode.value == '') {
            alert('验证码不能为空')
        }
        else if ((vcode.value).toLowerCase!==capcode) {
            alert('验证码不一致')
        }
        else if (password.value == '') {
            alert('密码不能为空')
        }
        else if (!validator.checkPwd(password.value)) {
            alert('密码格式不对')
        }
        else if (realpwd.value == '') {
            alert('确认密码不能为空')
        }
        else if (realpwd.value !== password.value) {
            alert('确认密码不正确')
        }
        else {
            https.post('/api/user/isExist', {
               account:account.value
            }, function (res) {
                if (res.errno == 0) {
                    https.post('/api/user/register', {
                        account: account.value,
                        password:password.value
                    }, function (res) {
                        if (res.errno == 0) {
                            // localStorage.set('user', res.data);
                            window.location.href='/login.html'
                        }
                    })
                } else {
                    console.log(res.message)
                }
           })
        }
    })
})